//
//  ViewController.m
//  WaterPull
//
//  Created by user on 16/4/22.
//  Copyright © 2016年 user. All rights reserved.
//

#import "ViewController.h"
#import "ShopCollectionViewCell.h"
#import "WaterLayerOut.h"
#import "Shop.h"
#import "MJExtension.h"
#import "MJRefresh.h"
@interface ViewController ()<UICollectionViewDataSource,WaterlayerDelagate>
@property(nonatomic,strong)UICollectionView *showCollection;
/** 所有的商品数据 */
@property (nonatomic, strong) NSMutableArray *shops;
@end

@implementation ViewController
 static  NSString   *inderfier = @"shop";
- (NSMutableArray *)shops
{
    if (!_shops) {
        _shops  = [NSMutableArray array];
    }
    return _shops;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    
    [self setupRefresh];
}
-(void)setUpUI
{
    
    WaterLayerOut *LayOut = [[WaterLayerOut alloc]init];
    LayOut.delegate = self;


 
    
    
    self.showCollection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:LayOut];
    self.showCollection.dataSource =self;
    self.showCollection.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.showCollection];
    [self.showCollection registerNib:[UINib nibWithNibName:NSStringFromClass([ShopCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:inderfier];
    
}
- (void)setupRefresh
{
    self.showCollection.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewShops)];
    [self.showCollection.header beginRefreshing];
    
    self.showCollection.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
    self.showCollection.footer.hidden = YES;
}
- (void)loadNewShops
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [Shop objectArrayWithFilename:@"1.plist"];
        [self.shops removeAllObjects];
        [self.shops addObjectsFromArray:shops];
        
        // 刷新数据
        [self.showCollection reloadData];
        
        [self.showCollection.header endRefreshing];
    });
}
- (void)loadMoreShops
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [Shop objectArrayWithFilename:@"1.plist"];
        [self.shops addObjectsFromArray:shops];
        
        // 刷新数据
        [self.showCollection reloadData];
        
        [self.showCollection.footer endRefreshing];
    });
}
#pragma mark --- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.showCollection.footer.hidden = self.shops.count == 0;
    return self.shops.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ShopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:inderfier forIndexPath:indexPath];
    cell.shop = self.shops[indexPath.item];
    return cell;
}

- (CGFloat)Waterlayer:(WaterLayerOut *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth{
     Shop *shop = self.shops[index];
    return itemWidth*shop.h/shop.w;
}
- (CGFloat)columnCountInWaterflowLayout:(WaterLayerOut *)waterflowLayout{
    return 3;
}
- (CGFloat)columnMarginInWaterflowLayout:(WaterLayerOut *)waterflowLayout{
    return 10;
}
- (CGFloat)rowMarginInWaterflowLayout:(WaterLayerOut *)waterflowLayout{
    return 10;
}
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(WaterLayerOut *)waterflowLayout{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}






@end













